function sqliteTools() {

	// 数据库名称
	var sqliteName = "fse";
	// 数据库存储路径
	var sqlitePath = "_doc/db/fse.db";

	var _this = this;

	/**
	 * 打开数据库
	 */
	this.openDatabase = function(callBack) {
		plus.sqlite.openDatabase({
			name: sqliteName,
			path: sqlitePath,
			success: function(e) {
				console.log('openDatabase success!');
				typeof callBack === "function" ? callBack() : false;
			},
			fail: function(e) {
				console.log('openDatabase failed: ' + JSON.stringify(e));
			}
		});
	}

	/**
	 * 关闭数据库
	 */
	this.closeDB = function() {
		plus.sqlite.closeDatabase({
			name: sqliteName,
			success: function(e) {
				console.log('closeDatabase success!');
			},
			fail: function(e) {
				console.log('closeDatabase failed: ' + JSON.stringify(e));
			}
		});
	}

	/**
	 * 判断数据库是否打开
	 */
	this.isOpenDatabase = function() {
		return plus.sqlite.isOpenDatabase({
			name: sqliteName,
			path: sqlitePath
		});
	}

	/**
	 * 初始化数据库表结构
	 */
	this.initSqlite = function() {
		var sqlArr = [];
		/*  
			serviceNo,
			serviceNoticeId,
			planId,
			customerName,
			instrumentId,
			instSn,
			acceptTime,
			fseId,
			fseName,
			serviceType,
			subServiceType,

			signIn,
			signInTime,
			signInLon,
			signInLat,
			signOff,
			signOffTime,
			signOffLon,
			signOffLat,
			estimatedVisitingTime,
			estimatedWorkingHours,
			estimatedDepartureTime,

			newInstSn,
			collateDate
		
			departureTime
			startTime
			endTime
			workHour
			journeyHour
			workContent
			workLogCreateTime

			createTime,
			updateTime,
			uploadStatus,
			delFlag // 现在用来标识已操作过的数据，原本想用来标识已删除的数据
		*/
		sqlArr.push(
			'CREATE TABLE IF NOT EXISTS t_service ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "serviceNo" TEXT, "serviceNoticeId" TEXT, "planId" TEXT, "customerName" TEXT, "instrumentId" TEXT, "instSn" TEXT, "acceptTime" TEXT, "fseId" TEXT, "fseName" TEXT, "serviceType" TEXT, "subServiceType" TEXT, "signIn" TEXT, "signInTime" TEXT, "signInLon" TEXT, "signInLat" TEXT, "signOff" TEXT, "signOffTime" TEXT, "signOffLon" TEXT, "signOffLat" TEXT, "estimatedVisitingTime" TEXT, "estimatedWorkingHours" TEXT, "estimatedDepartureTime" TEXT, "newInstSn" TEXT, "collateDate" TEXT, "departureTime" TEXT, "startTime" TEXT, "endTime" TEXT, "workHour" TEXT, "journeyHour" TEXT, "workContent" TEXT, "workLogCreateTime" TEXT, "createTime" TEXT, "updateTime" TEXT, "uploadStatus" TEXT, "delFlag" TEXT)'
		);

		plus.sqlite.executeSql({
			name: sqliteName,
			sql: sqlArr,
			success: function(e) {
				console.log('createTables executeSql success!');
				_this.closeDB();
			},
			fail: function(e) {
				console.log('createTables executeSql failed: ' + JSON.stringify(e));
				_this.closeDB();
			}
		});
	}
	
	// 查询所有未上传的数据
	this.selectService = function(enginnerId, callBack) {
		// var sql = "select * from t_service where fseId = '" + enginnerId + "' and uploadStatus = '0' and delFlag = '0'"
		var sql = "select * from t_service where fseId = '" + enginnerId + "' and uploadStatus = '0'"
		plus.sqlite.selectSql({
			name: sqliteName,
			sql: sql,
			success: function(data) {
				console.log('selectSql success: ' + JSON.stringify(data));
				_this.closeDB()
				callBack(true, data);
			},
			fail: function(e) {
				console.log('selectSql failed: ' + JSON.stringify(e));
				_this.closeDB()
				callBack(false, e);
			}
		});
	}
	
	// 查询所有已操作，可上传的数据 delFlag = '1' 代表数据已操作
	this.selectUploadService = function(enginnerId, callBack) {
		var sql = "select * from t_service where fseId = '" + enginnerId + "' and uploadStatus = '0' and delFlag = '1'"
		plus.sqlite.selectSql({
			name: sqliteName,
			sql: sql,
			success: function(data) {
				console.log('selectSql success: ' + JSON.stringify(data));
				// _this.closeDB()
				callBack(true, data);
			},
			fail: function(e) {
				console.log('selectSql failed: ' + JSON.stringify(e));
				// _this.closeDB()
				callBack(false, e);
			}
		});
	}
	
	// 通过planId查询
	this.selectServiceByPlanId = function(enginnerId, planId, resData, callBack) {
		/* var sql = "select * from t_service where fseId = '" + enginnerId +
			"' and uploadStatus = '0' and delFlag = '0' and planId in (" + planId + ")" */
		var sql = "select * from t_service where fseId = '" + enginnerId + "'and planId in (" + planId + ")"
		plus.sqlite.selectSql({
			name: sqliteName,
			sql: sql,
			success: function(data) {
				console.log('selectSql success: ' + JSON.stringify(data));
				// _this.closeDB()
				callBack(true, data, resData);
			},
			fail: function(e) {
				console.log('selectSql failed: ' + JSON.stringify(e));
				// _this.closeDB()
				callBack(false);
			}
		});
	}
	
	// 通过serviceNoticeId查询
	this.selectServiceByNoticeId = function(enginnerId, serviceNoticeId, resData, callBack) {
		var sql = "select * from t_service where fseId = '" + enginnerId +
			"' and serviceNoticeId in (" + serviceNoticeId + ")"
		plus.sqlite.selectSql({
			name: sqliteName,
			sql: sql,
			success: function(data) {
				console.log('selectSql success: ' + JSON.stringify(data));
				callBack(true, data, resData);
			},
			fail: function(e) {
				console.log('selectSql failed: ' + JSON.stringify(e));
				callBack(false);
			}
		});
	}

	/**
	 * 添加服务基本数据 如：服务号，计划id，接收任务时间等等
	 */
	this.insertServiceBaseData = function(serviceNo, serviceNoticeId, planId, customerName, instrumentId, instSn,
		acceptTime, fseId, fseName, serviceType, subServiceType, callBack) {
		var createTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "insert into t_service('serviceNo', 'serviceNoticeId', 'planId', 'customerName', 'instrumentId', 'instSn', 'acceptTime', 'fseId', 'fseName', 'serviceType', 'subServiceType', 'createTime', 'uploadStatus', 'delFlag') values('" +
				serviceNo + "', '" + serviceNoticeId + "', '" + planId + "', '" + customerName + "', '" + instrumentId + "', '" +
				instSn + "', '" + acceptTime + "', '" + fseId + "', '" + fseName + "', '" + serviceType + "', '" + subServiceType + "', '" + createTime +
				"', '0', '0')",
			success: function(e) {
				console.log('insertServiceBaseData executeSql success!');
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceBaseData executeSql failed: ' + JSON.stringify(e));
				callBack(false)
			}
		});
	}
	
	/**
	 * 签到后若本地库不存在该服务，添加该服务基本数据及签到数据
	 */
	this.insertServiceBaseAndSignInData = function(serviceNo, serviceNoticeId, planId, customerName, instrumentId, instSn,
		acceptTime, fseId, fseName, serviceType, subServiceType, signIn, signInTime, signInLon, signInLat, callBack) {
		var createTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "insert into t_service('serviceNo', 'serviceNoticeId', 'planId', 'customerName', 'instrumentId', 'instSn', 'acceptTime', 'fseId', 'fseName', 'serviceType', 'subServiceType', 'signIn', 'signInTime', 'signInLon', 'signInLat', 'createTime', 'uploadStatus', 'delFlag') values('" +
				serviceNo + "', '" + serviceNoticeId + "', '" + planId + "', '" + customerName + "', '" + instrumentId + "', '" +
				instSn + "', '" + acceptTime + "', '" + fseId + "', '" + fseName + "', '" + serviceType + "', '" + subServiceType + "', '" + signIn + "', '" + signInTime + "', '" + signInLon + "', '" + signInLat + "', '" + createTime +
				"', '0', '0')",
			success: function(e) {
				console.log('insertServiceBaseAndSignInData executeSql success!');
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceBaseAndSignInData executeSql failed: ' + JSON.stringify(e));
				callBack(false)
			}
		});
	}

	/**
	 * 添加签到数据 delFlag = '1' 代表数据已操作
	 */
	this.insertServiceSignInData = function(id, signIn, signInTime, signInLon, signInLat, delFlag, callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set signIn = '" + signIn + "', signInTime = '" + signInTime + "', signInLon = '" +
				signInLon +
				"', signInLat = '" + signInLat + "', updateTime = '" + updateTime + "', uploadStatus = '0', delFlag = '" + delFlag + "' where id = '" + id + "'",
			success: function(e) {
				console.log('insertServiceSignInData executeSql success!');
				// _this.closeDB()
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceSignInData executeSql failed: ' + JSON.stringify(e));
				// _this.closeDB()
				callBack(false)
			}
		});
	}

	/**
	 * 添加签离数据
	 */
	/* this.insertServiceSignOffData = function(id, signOff, signOffTime, signOffLon, signOffLat, estimatedVisitingTime,
		estimatedWorkingHours, estimatedDepartureTime, callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set signOff = '" + signOff + "', signOffTime = '" + signOffTime + "', signOffLon = '" +
				signOffLon + "', signOffLat = '" + signOffLat + "', estimatedVisitingTime = '" + estimatedVisitingTime +
				"', estimatedWorkingHours = '" + estimatedWorkingHours + "', estimatedDepartureTime = '" +
				estimatedDepartureTime +
				"', updateTime = '" + updateTime + "'  where id = '" + id + "'",
			success: function(e) {
				console.log('insertServiceSignOffData executeSql success!');
				_this.closeDB()
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceSignOffData executeSql failed: ' + JSON.stringify(e));
				_this.closeDB()
				callBack(false)
			}
		});
	} */

	/**
	 * 添加核对数据 delFlag = '1' 代表数据已操作
	 */
	this.insertServiceCollateData = function(id, newInstSn, callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set newInstSn = '" + newInstSn + "', collateDate = '" + updateTime + "' , updateTime = '" +
				updateTime + "', delFlag = '1' where id = '" + id +
				"'",
			success: function(e) {
				console.log('insertServiceCollateData executeSql success!');
				_this.closeDB()
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceCollateData executeSql failed: ' + JSON.stringify(e));
				_this.closeDB()
				callBack(false)
			}
		});
	}

	/**
	 * 添加工作日志 delFlag = '1' 代表数据已操作
	 */
	this.insertServiceWorkingLogData = function(id, departureTime, startTime, endTime, workHour, journeyHour, workContent,
		callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set departureTime = '" + departureTime + "', startTime = '" + startTime + "', endTime = '" +
				endTime + "', workHour = '" + workHour +
				"', journeyHour = '" + journeyHour + "', workContent = '" + workContent + "', workLogCreateTime = '" +
				updateTime + "', updateTime = '" + updateTime +
				"', delFlag = '1' where id = '" + id + "'",
			success: function(e) {
				console.log('insertServiceWorkingLogData executeSql success!');
				_this.closeDB()
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceWorkingLogData executeSql failed: ' + JSON.stringify(e));
				_this.closeDB()
				callBack(false)
			}
		});
	}
	
	/**
	 * 添加再次上门更新planId数据
	 */
	this.insertServiceSignOffAginData = function(id, planId, callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set signIn = '0', signInTime = '', planId = '" + planId + "', updateTime = '" + updateTime + 
				"', uploadStatus = '0', delFlag = '0' where id = '" + id + "'",
			success: function(e) {
				console.log('insertServiceSignOffAginData executeSql success!');
				callBack(true)
			},
			fail: function(e) {
				console.log('insertServiceSignOffAginData executeSql failed: ' + JSON.stringify(e));
				callBack(false)
			}
		});
	}

	/**
	 * 上传数据成功
	 */
	this.uploadServiceSuccessData = function(id, callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set uploadStatus = '1', updateTime = '" + updateTime + "' where id = '" + id + "'",
			success: function(e) {
				console.log('uploadServiceSuccessData executeSql success!');
				// _this.closeDB()
				callBack(true, id)
			},
			fail: function(e) {
				console.log('uploadServiceSuccessData executeSql failed: ' + JSON.stringify(e));
				// _this.closeDB()
				callBack(false)
			}
		});
	}

	/**
	 * 删除数据
	 */
	this.deleteServiceData = function(planId, callBack) {
		var updateTime = new Date().Format("yyyy-MM-dd HH:mm:ss");
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "update t_service set uploadStatus = '1', updateTime = '" + updateTime + "' where planId = '" + planId + "'",
			success: function(e) {
				console.log('deleteServiceData executeSql success!');
				callBack(true)
			},
			fail: function(e) {
				console.log('deleteServiceData executeSql failed: ' + JSON.stringify(e));
				callBack(false)
			}
		});
	}
	
	/**
	 * 清空表数据
	 */
	this.clearData = function() {
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "delete from t_service; select * from sqlite_sequence; update sqlite_sequence set seq=0 where name='t_service'",
			success: function(e) {
				console.log('clearData executeSql success!');
				_this.closeDB();
			},
			fail: function(e) {
				console.log('clearData executeSql failed: ' + JSON.stringify(e));
				_this.closeDB();
			}
		});
	}
	
	/**
	 * 删除表
	 */
	this.dropTable = function() {
		plus.sqlite.executeSql({
			name: sqliteName,
			sql: "drop table t_service",		
			success: function(e) {
				console.log('dropTable executeSql success!');
				// _this.closeDB();
			},
			fail: function(e) {
				console.log('dropTable executeSql failed: ' + JSON.stringify(e));
				// _this.closeDB();
			}
		});
	}	
}
